package com.ruoyi.gpt.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gpt.domain.vo.GptGoodsVo;
import com.ruoyi.gpt.domain.bo.GptGoodsBo;
import com.ruoyi.gpt.service.IGptGoodsService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * Gpt商品列表
 *
 * @author ztx
 * @date 2023-04-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/gpt/gptGoods")
public class GptGoodsController extends BaseController {

    private final IGptGoodsService iGptGoodsService;

    /**
     * 查询Gpt商品列表列表
     */
    @SaCheckPermission("gpt:gptGoods:list")
    @GetMapping("/list")
    public TableDataInfo<GptGoodsVo> list(GptGoodsBo bo, PageQuery pageQuery) {
        return iGptGoodsService.queryPageList(bo, pageQuery);
    }
    /**
     * 手机端查询Gpt商品列表列表
     */
    @GetMapping("/listForApp")
    public TableDataInfo<GptGoodsVo> listForApp(GptGoodsBo bo, PageQuery pageQuery) {
        return iGptGoodsService.queryListForApp(bo, pageQuery);
    }
    /**
     * 导出Gpt商品列表列表
     */
    @SaCheckPermission("gpt:gptGoods:export")
    @Log(title = "Gpt商品列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(GptGoodsBo bo, HttpServletResponse response) {
        List<GptGoodsVo> list = iGptGoodsService.queryList(bo);
        ExcelUtil.exportExcel(list, "Gpt商品列表", GptGoodsVo.class, response);
    }

    /**
     * 获取Gpt商品列表详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("gpt:gptGoods:query")
    @GetMapping("/{id}")
    public R<GptGoodsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iGptGoodsService.queryById(id));
    }

    /**
     * 新增Gpt商品列表
     */
    @SaCheckPermission("gpt:gptGoods:add")
    @Log(title = "Gpt商品列表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody GptGoodsBo bo) {
        return toAjax(iGptGoodsService.insertByBo(bo));
    }

    /**
     * 修改Gpt商品列表
     */
    @SaCheckPermission("gpt:gptGoods:edit")
    @Log(title = "Gpt商品列表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody GptGoodsBo bo) {
        return toAjax(iGptGoodsService.updateByBo(bo));
    }
    /**
     * 修改Gpt商品状态
     */
    @SaCheckPermission("gpt:gptGoods:edit")
    @Log(title = "Gpt商品列表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/shelf")
    public R<Void> editShelf(@RequestBody GptGoodsBo bo) {
        if(!"1".equals(bo.getShelf()) && !"0".equals(bo.getShelf())){
            return R.fail("参数错误");
        }
        if(ObjectUtil.isNull(bo.getId())){
            return R.fail("id不能为空");
        }
        Long id = bo.getId();
        GptGoodsVo gptGoodsVo = iGptGoodsService.queryById(id);
        gptGoodsVo.setShelf(bo.getShelf());
        return toAjax(iGptGoodsService.updateByBo(BeanUtil.toBean(gptGoodsVo,GptGoodsBo.class)));
    }

    /**
     * 删除Gpt商品列表
     *
     * @param ids 主键串
     */
    @SaCheckPermission("gpt:gptGoods:remove")
    @Log(title = "Gpt商品列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iGptGoodsService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
